﻿using System;
using System.Collections.Generic;
using System.Text;

namespace Safebrowsing
{
    /// <summary>
    /// Part of the response for data request.
    /// </summary>
    public class DataListResponse
    {
        /// <summary>
        /// Creates an empty instance of the DataListResponse
        /// </summary>
        internal DataListResponse(string listName)
        {
            List = SafebrowsingList.GetListByName(listName);
            if (List == null)
            {
                throw new ApplicationException("There is not list with name " + listName);
            }

            RedirectUrls = new List<Uri>();
            AddChunksToDelete = new List<int>();
            SubChunksToDelete = new List<int>();
        }

        /// <summary>
        /// Safebrowsing list
        /// </summary>
        public SafebrowsingList List { get; set; }

        /// <summary>
        /// List of "redirect url". We can get list data itself requesting to these redirect urls.
        /// </summary>
        public List<Uri> RedirectUrls { get; set; }

        /// <summary>
        /// List of add chunks numbers to delete
        /// </summary>
        public List<int> AddChunksToDelete { get; set; }

        /// <summary>
        /// List of sub chunks numbers to delete
        /// </summary>
        public List<int> SubChunksToDelete { get; set; }

        public override string ToString()
        {
            var sb = new StringBuilder();
            sb.AppendFormat("List name: {0}", List);
            sb.AppendLine();
            sb.AppendLine("Redirect URLs:");
            {
                foreach (Uri uri in RedirectUrls)
                {
                    sb.Append("    ");
                    sb.AppendLine(uri.ToString());
                }
            }
            if (AddChunksToDelete.Count > 0)
            {
                sb.Append("Add-chunks to delete: ");
                sb.AppendLine(Utils.Join(',', AddChunksToDelete));                
            }
            if (SubChunksToDelete.Count > 0)
            {
                sb.Append("Sub-chunks to delete: ");
                sb.AppendLine(Utils.Join(',', SubChunksToDelete));
            }

            return sb.ToString();
        }
    }
}
